package cn.net.zj.scan.ui;

import android.app.DatePickerDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.device.ScanDevice;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.net.zj.scan.R;
import cn.net.zj.scan.base.CommonActivity;

public class OrderBrowserConditionActivity extends CommonActivity {

    //<editor-fold desc=”成员变量”>

    @BindView(R.id.spType)
    Spinner spType;
    @BindView(R.id.tvBegin)
    TextView tvBegin;
    @BindView(R.id.tvEnd)
    TextView tvEnd;
    @BindView(R.id.etBillNumber)
    EditText etBillNumber;


    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    private int spSelect = 0;

    private ScanDevice sm;

    private final static String SCAN_ACTION = "com.action.SCAN_RESULT";

    //</editor-fold>

    //<editor-fold desc=”生命周期”>
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_browser_condition);
        ButterKnife.bind(this);
        configData();
        configUI();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (sm != null) {
            sm.stopScan();
        }
        unregisterReceiver(mScanReceiver);
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter();
        filter.addAction(SCAN_ACTION);
        registerReceiver(mScanReceiver, filter);
    }

    //</editor-fold>


    //<editor-fold desc=”私有方法”>
    private void configData() {
        try {
            sm = new ScanDevice();
            sm.setOutScanMode(ScanDevice.SCANNER_MODE_BROADCAST);
            sm.openScan();
        } catch (Exception e) {
            cn.net.zj.scan.utils.ToastUtils.showToast("不支持此手机,请用指定的终端机扫码");
        }
    }

    private void configUI() {
        tvBegin.setText(TimeUtils.date2String(new Date(), dateFormat));
        tvEnd.setText(TimeUtils.date2String(new Date(), dateFormat));


        spType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                spSelect = i;
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    @OnClick({R.id.tvBegin, R.id.tvEnd, R.id.btQuery})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBegin: {
                Calendar c = Calendar.getInstance();
                int mYear = c.get(Calendar.YEAR);
                int mMonth = c.get(Calendar.MONTH);
                int mDay = c.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                        StringBuffer sb = new StringBuffer();
                        sb.append(i);
                        sb.append("-");
                        sb.append(i1 + 1);
                        sb.append("-");
                        sb.append(i2);
                        tvBegin.setText(sb);
                    }
                }, mYear, mMonth, mDay);
                datePickerDialog.show();
            }
            break;
            case R.id.tvEnd: {
                Calendar c = Calendar.getInstance();
                int mYear = c.get(Calendar.YEAR);
                int mMonth = c.get(Calendar.MONTH);
                int mDay = c.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                        StringBuffer sb = new StringBuffer();
                        sb.append(i);
                        sb.append("-");
                        sb.append(i1 + 1);
                        sb.append("-");
                        sb.append(i2);
                        tvEnd.setText(sb);
                    }
                }, mYear, mMonth, mDay);
                datePickerDialog.show();
            }
            break;
            case R.id.btQuery: {

                Date begin = TimeUtils.string2Date(tvBegin.getText().toString(), dateFormat);
                Date end = TimeUtils.string2Date(tvEnd.getText().toString(), dateFormat);
                if (begin.after(end)) {
                    ToastUtils.showShort("开始时间不能大于结束时间!");
                    return;
                }

                Intent intent = new Intent(this, TaskListActivity.class);
                intent.putExtra("r_icmobill", etBillNumber.getText().toString());
                intent.putExtra("start_time", tvBegin.getText().toString());
                intent.putExtra("end_time", tvEnd.getText().toString());
                intent.putExtra("ftype", spSelect == 0 ? "未完成" : "已完成");

                ActivityUtils.startActivity(intent);
            }
            break;
        }
    }

    private void processResult(String resultData) {
        if (resultData.startsWith("R_")) {
            etBillNumber.setText(resultData);
        }
    }

    //</editor-fold>

    //<editor-fold desc=”内部类”>

    private BroadcastReceiver mScanReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            byte[] barcodeArray = intent.getByteArrayExtra("scanContext");
            String barcodeStr = new String(barcodeArray, 0, barcodeArray.length);
            processResult(barcodeStr);
            sm.stopScan();
        }
    };
    //</editor-fold>

}
